angular.module('mamApp').controller('cpOverviewController', ['$scope', 'DataManager', '$state','api','paramsManager',
    function ($scope, DataManager, $state,api,paramsManager) {
        $scope.search = {
            pageNo: $state.params.pageNo || 1,
            pageSize: $state.params.pageSize || 10,
            startTime: $state.params.startTime || "",
            endTime: $state.params.endTime || "",
            "X-Auth-Token": $scope.userInfo.token

        }


        $scope.paginationConf = {
            currentPage: $state.params.pageNo || 1,
            itemsPerPage: $state.params.pageSize || 10,
            pagesLength: 9,
            search: 0,
            onChange: function () {
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search)
            }
        }
        $scope.downHref=api+"/admin/exportCpCount"+paramsManager.query($scope.search);

        $scope.rangeDate = {
            startDate: $scope.search.startTime ? moment($scope.search.startTime).format('X') : null,
            endDate: $scope.search.endTime ? moment($scope.search.endTime).format('X') : null,
            onSelected: function (data, scope) {  //时间选择确认事件
                $scope.search.startTime = data.startDate ? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD") : null;
                $scope.search.endTime = data.endDate ? moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD") : null;
                $scope.search.pageNo=1;
                $state.go($state.current.name, $scope.search);

            }
        };
        $scope.noPermission=false;

        DataManager.cpCountStat([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
            if(result.data.c=="n"){
                $scope.noPermission=true;
                return false;
            }
            $scope.list = result.data.pageList;
            var list = angular.copy($scope.list).reverse();
            var xAxisData = [];
            var data1 = [];
            var data2 = [];
            var data3 = [];
            var data4 = [];
            var data5 = [];
            var data6 = [];
            var data7 = [];
            var data8 = [];
            var data9 = [];
            var data10 = [];
            var data11 = [];
            var data12 = [];
            list.forEach(function (item) {
                xAxisData.push(item.countTime);
                data1.push(item.cpNum);
                data2.push(item.lvOneNum);
                data3.push(item.lvTwoNum);
                data4.push(item.lvThreeNum);
                data5.push(item.reviewWatingNum);
                data6.push(item.reviewFailNum);
                data7.push(item.reviewPassNum);
                data8.push(item.statusNotEnableNum);
                data9.push(item.statusForbidNum);
                data10.push(item.statusEnableNum);
                data11.push(item.coopTypeSignedNum);
                data12.push(item.coopTypeNotSignedNum);
            })
            var seriesData = [data1, data2, data3, data4, data5, data6, data7, data8, data9, data10, data11, data12];
            initData(xAxisData, seriesData);
            $scope.total = result.data.totalCount;
            $scope.paginationConf.totalItems = result.data.total;
            var cpLvData=[$scope.total.lvOneNum,$scope.total.lvTwoNum,$scope.total.lvThreeNum];
            cpLvView(cpLvData);
            var cpReviewData=[$scope.total.reviewWatingNum,$scope.total.reviewFailNum,$scope.total.reviewPassNum];
            cpReviewView(cpReviewData);
            var cpStatusData=[$scope.total.statusNotEnableNum,$scope.total.statusForbidNum,$scope.total.statusEnableNum];
            cpStatusView(cpStatusData);
            var cpCoopTypeData=[$scope.total.coopTypeSignedNum,$scope.total.coopTypeNotSignedNum];
            cpCoopTypeView(cpCoopTypeData);
        })


        function initData(xAxisData, seriesData) {
            $scope.option = {
                title: {
                    text: 'CP概况'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                legend: {
                    data: ['CP总量', 'LV1', 'LV2', 'LV3', '认证-待审核', '认证-未通过', '认证-通过', '功能-待启用', '功能-禁用', '功能-启用', '已签约', '未签约'],
                    bottom:"0"
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '6%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        data: xAxisData
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: 'CP总量',
                        type: 'bar',
                        data: seriesData[0],
                        markLine: {
                            lineStyle: {
                                normal: {
                                    type: 'dashed'
                                }
                            },
                            data: [
                                [{type: 'min'}, {type: 'max'}]
                            ]
                        }
                    },
                    {
                        name: 'LV1',
                        type: 'bar',
                        stack: 'CP等级',
                        data: seriesData[1]
                    },
                    {
                        name: 'LV2',
                        type: 'bar',
                        stack: 'CP等级',
                        data: seriesData[2]
                    },
                    {
                        name: 'LV3',
                        type: 'bar',
                        stack: 'CP等级',
                        data: seriesData[3]
                    },
                    {
                        name: '认证-待审核',
                        type: 'bar',
                        stack: '认证状态',
                        data: seriesData[4],

                    },
                    {
                        name: '认证-未通过',
                        type: 'bar',
                        stack: '认证状态',
                        data: seriesData[5]
                    },
                    {
                        name: '认证-通过',
                        stack: '认证状态',
                        type: 'bar',
                        data: seriesData[6]
                    },
                    {
                        name: '功能-待启用',
                        type: 'bar',
                        stack: '功能状态',
                        data: seriesData[7]
                    },
                    {
                        name: '功能-禁用',
                        type: 'bar',
                        stack: '功能状态',
                        data: seriesData[8]
                    },
                    {
                        name: '功能-启用',
                        type: 'bar',
                        stack: '功能状态',
                        data: seriesData[9]
                    },
                    {
                        name: '已签约',
                        type: 'bar',
                        stack: '签约状态',
                        data: seriesData[10]
                    },
                    {
                        stack: '签约状态',
                        name: '未签约',
                        type: 'bar',
                        data: seriesData[11]
                    }
                ]
            };
        }

        //饼图
         function cpLvView(cpLvData) {
            $scope.cpLvView = {
                title: {
                    text: ' CP等级',
                    x: 'center',
                    textStyle:{
                        fontSize:16,
                        fontWeight:500,
                        color:'#000',
                        fontFamily:'Microsoft Yahei',
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: ['Lv1', 'Lv2', 'Lv3']
                },
                series: [
                    {
                        name: 'CP等级',
                        type: 'pie',
                        radius: '50%',
                        center: ['50%', '60%'],
                        data: [
                            {
                                name:'Lv1',
                                value:cpLvData[0]
                            },
                            {
                                name:'Lv2',
                                value:cpLvData[1]
                            },
                            {
                                name:'Lv3',
                                value:cpLvData[2]
                            }
   
                        ],

                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        },
                         color:['#4285f4', '#ea4335','#fbbc05']
                    }
                ]
            };
        }

         function cpReviewView(cpReviewData) {
            $scope.cpReviewView = {
                title: {
                    text: ' CP认证',
                    x: 'center',
                    textStyle:{
                        fontSize:16,
                        fontWeight:500,
                        color:'#000',
                        fontFamily:'Microsoft Yahei',
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: ['待审核', '未通过', '通过']
                },
                series: [
                    {
                        name: 'CP认证',
                        type: 'pie',
                        radius: '50%',
                        center: ['50%', '60%'],
                        data: [
                            {
                                name:'待审核',
                                value:cpReviewData[0]
                            },
                            {
                                name:'未通过',
                                value:cpReviewData[1]
                            },
                            {
                                name:'通过',
                                value:cpReviewData[2]
                            }
   
                        ],

                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        },
                         color:['#4285f4', '#ea4335','#fbbc05']
                    }
                ]
            };
        }

       
        function cpStatusView(cpStatusDate) {
            $scope.cpStatusView = {
                title: {
                    text: ' CP功能',
                    x: 'center',
                    textStyle:{
                        fontSize:16,
                        fontWeight:500,
                        color:'#000',
                        fontFamily:'Microsoft Yahei',
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: ['待启用', '禁用', '启用']
                },
                series: [
                    {
                        name: 'CP功能',
                        type: 'pie',
                        radius: '50%',
                        center: ['50%', '60%'],
                        data: [
                            {
                                name:'待启用',
                                value:cpStatusDate[0]
                            },
                            {
                                name:'禁用',
                                value:cpStatusDate[1]
                            },
                            {
                                name:'启用',
                                value:cpStatusDate[2]
                            }
   
                        ],

                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        },
                        color:['#4285f4', '#ea4335','#fbbc05']
                    }
                ]
            };
        }

        function cpCoopTypeView(cpCoopTypeData) {
            $scope.cpCoopTypeView = {
                title: {
                    text: ' CP签约',
                    x: 'center',
                    textStyle:{
                        fontSize:16,
                        fontWeight:500,
                        color:'#000',
                        fontFamily:'Microsoft Yahei',
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: ['已签约', '未签约']
                },
                series: [
                    {
                        name: 'CP签约',
                        type: 'pie',
                        radius: '50%',
                        center: ['50%', '60%'],
                        data: [
                            {
                                name:'已签约',
                                value:cpCoopTypeData[0]
                            },
                            {
                                name:'未签约',
                                value:cpCoopTypeData[1]
                            },
                        ],

                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        },
                        color:['#4285f4', '#fbbc05']
                    }
                ]
            };
        }
    }])

